/*
 * Copyright © 2008-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/time.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>

#include <modbus.h>

#include "myjni.h"
#define G_MSEC_PER_SEC 1000

static uint32_t gettime_ms(void)
{
    struct timeval tv;
#if !defined(_MSC_VER)
    gettimeofday(&tv, NULL);
    return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
#else
    return GetTickCount();
#endif
}

enum {
    TCP,
    RTU
};

/* Tests based on PI-MBUS-300 documentation */
int modbus_read(int addr, int len, uint16_t *data)
{
    uint8_t *tab_bit;
    uint16_t *tab_reg;
    modbus_t *ctx;
    int i;
    int nb_points;
    double elapsed;
    uint32_t start;
    uint32_t end;
    uint32_t bytes;
    uint32_t rate;
    int rc;

    return 0;

    ctx = modbus_new_rtu("/dev/ttyHSL1", 9600, 'N', 8, 1);
    modbus_set_slave(ctx, 0x50);
    if (modbus_connect(ctx) == -1) {
        LOGE("Connection failed: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }
    rc = modbus_set_response_timeout(ctx, 0, 50000);
    rc = modbus_read_registers(ctx, addr, len, data);

    /* Close the connection */
    modbus_close(ctx);
    modbus_free(ctx);
    
    return rc;

}
